package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.MainActivity;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 闪屏（启动页）
 */
public class SplashActivity extends BaseActivity {

    @BindView(R.id.iv_entry)
    ImageView mIVEntry;
    private static final int ANIM_TIME = 3000;
    private static final float SCALE_END = 1.20F;
    @BindView(R.id.btn_jump)
    TextView btnJump;
    private Handler mhandler = new Handler();
    private Runnable mRunable;
    private boolean isMember;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         isMember = (boolean) SPUtils.get(SplashActivity.this, Constant.IS_MEMBER, false);

        //跳转到首页
        mRunable = new Runnable() {
            @Override
            public void run() {
                boolean isLogin = (boolean) SPUtils.get(SplashActivity.this, Constant.isLogin, false);
                if (isLogin) {
                    if(isMember){
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    }else{
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    }
                } else {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                }

                SplashActivity.this.finish();
            }
        };

        // 判断是否是第一次开启应用
        boolean isFirstOpen = (boolean) SPUtils.get(SplashActivity.this, Constant.First_START, true);
        // 如果是第一次启动，则先进入功能引导页
        if (isFirstOpen) {
            Intent intent = new Intent(this, GuideActivity.class);
            startActivity(intent);
            finish();
            return;
        }
        // 如果不是第一次启动app，则正常显示启动屏
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        mhandler.postDelayed(mRunable, 2000);
        StatusBarUtil.setTranslucentForImageView(SplashActivity.this, 0, null);
        //startMainActivity();

    }

//    private void startMainActivity() {
//
//        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                startAnim();
//                //检查权限
//
//            }
//        });
//    }


//    private void startAnim() {
//
//        ObjectAnimator animatorX = ObjectAnimator.ofFloat(mIVEntry, "scaleX", 1f, SCALE_END);
//        ObjectAnimator animatorY = ObjectAnimator.ofFloat(mIVEntry, "scaleY", 1f, SCALE_END);
//
//        AnimatorSet set = new AnimatorSet();
//        set.setDuration(ANIM_TIME).play(animatorX).with(animatorY);
//        set.start();
//
//        set.addListener(new AnimatorListenerAdapter() {
//
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                boolean isLogin = (boolean) SPUtils.get(SplashActivity.this, Constant.isLogin, false);
//                if (isLogin) {
//                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
//                } else {
//                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
//                }
//
//                SplashActivity.this.finish();
//            }
//        });
//    }

    /**
     * 屏蔽物理返回按钮
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick(R.id.btn_jump)
    public void onViewClicked() {
        mhandler.removeCallbacks(mRunable);
        boolean isLogin = (boolean) SPUtils.get(SplashActivity.this, Constant.isLogin, false);
        if (isLogin) {
            if(isMember){
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }else{
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
        } else {
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        }
        SplashActivity.this.finish();
    }

    //---------------------------------------------------------
}
